package com.example.yeujian5.yuejian5.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.yeujian5.yuejian5.R;
import com.example.yeujian5.yuejian5.adapter.QuestionAdapter;
import com.example.yeujian5.yuejian5.bean.QuestionBean;
import com.example.yeujian5.yuejian5.model.MasterModel;
import com.example.yeujian5.yuejian5.model.Model;
import com.example.yeujian5.yuejian5.service.MasterService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by wangmengmeng on 2017/10/24.
 */

public class QuestionFragment extends Fragment {

    private Call<List<QuestionBean>> call;
    private List<QuestionBean> list;
    private Context context;
    private RecyclerView recyclerView;
    private View view = null;
    private EditText et_ask;
    private Button bt_ask;
    int userid;
    String Id,content;
    SharedPreferences sp;
    Context Context;




    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_question,null);
        Context=getActivity();


          /*获取用户*/
        sp=Context.getSharedPreferences("Userid",MODE_PRIVATE);
        userid=Integer.valueOf(sp.getString("user_id",null));


        initView();
        initEvents();
        return view;
    }

    private void initEvents(){
        Retrofit retrofit= Model.getConn();
        MasterService masterService=retrofit.create(MasterService.class);
        call=masterService.getQuestionList(Id);
        call.enqueue(new Callback<List<QuestionBean>>() {
            @Override
            public void onResponse(Call<List<QuestionBean>> call, Response<List<QuestionBean>> response) {
                list=response.body();
                QuestionAdapter a=new QuestionAdapter(list,context);
                LinearLayoutManager linear=new LinearLayoutManager(context);
                linear.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(linear);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
                recyclerView.setAdapter(a);
            }

            @Override
            public void onFailure(Call<List<QuestionBean>> call, Throwable t) {
                Toast.makeText(context,"留言列表获取失败",Toast.LENGTH_SHORT).show();
            }
        });


    }




    private void initView(){

        context=getActivity();
        Intent intent=getActivity().getIntent();
        Id=intent.getStringExtra("id");
        recyclerView=(RecyclerView)view.findViewById(R.id.question_Rec);
        et_ask=(EditText)view.findViewById(R.id.ask_content);
        bt_ask=(Button)view.findViewById(R.id.ask_submit);
        content = "question";

        bt_ask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                content = et_ask.getText().toString();
                if (content.equals("")) {
                    Toast.makeText(getActivity(), "您输入的信息为空", Toast.LENGTH_LONG).show();
                } else {
                    MasterModel masterModel = new MasterModel();
                    Call call = masterModel.addQuestion(Id, userid,content);
                    call.enqueue(new Callback<Integer>() {
                        @Override
                        public void onResponse(Call<Integer> call, Response<Integer> response) {

                            if (response.body() > 0) {
                                Toast.makeText(getActivity(), "留言发布成功", Toast.LENGTH_LONG).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<Integer> call, Throwable t) {

                        }
                    });
                }
            }
        });

    }
}
